package com.fick.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.fick.util.General;

public class ResponseFiles extends HttpServlet {
	private static Logger log = Logger.getLogger(ResponseFiles.class);

	public ResponseFiles() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setHeader("charset", "utf-8");
		PrintWriter out = response.getWriter();
		if(request.getParameter("dic")!=null&&!request.getParameter("dic").equals("")){
			if (request.getParameter("o").equals("dic")) {// �����ļ���ʱ
				// response.setCharacterEncoding("utf-8");
				String dic = request.getParameter("dic");// ���=
				String filepic = request.getParameter("filepic");
				String dicpic = request.getParameter("dicpic");
				String prepic = request.getParameter("prepic");
				// log.error(ShowFiles.show(new File(dic), filepic, dicpic));
				out.println(CharEncoding.utfToIso(ShowFiles.show(new File(dic),
						filepic, dicpic, prepic)));
				out.flush();
				out.close();
			}else{// �����ļ�ʱ
				String filepath = request.getParameter("dic");
				FileInputStream is = new FileInputStream(filepath);
				byte buffer[] = new byte[is.available()];
				is.read(buffer);
				// log.error(new String(buffer));
				is.close();
				out.println((CharEncoding.utfToIso(new String(buffer))).replaceAll(
						"\"", " ").replaceAll("'", " "));// ����ļ�����
			}
		}
		

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setHeader("charset", "utf-8");
		PrintWriter out = response.getWriter();
		if (request.getParameter("o").equals("dic")) {// �����ļ���ʱ
			// response.setCharacterEncoding("utf-8");
			String dic = request.getParameter("dic");// ���Ҫ��ʾ���ļ���·��
			String filepic = request.getParameter("filepic");
			String dicpic = request.getParameter("dicpic");
			String prepic = request.getParameter("prepic");
			// log.error(ShowFiles.show(new File(dic), filepic, dicpic));
			out.println(CharEncoding.utfToIso(ShowFiles.show(new File(dic),
					filepic, dicpic, prepic)));
			out.flush();
			out.close();
		} else {// �����ļ�ʱ
			String filepath = request.getParameter("dic");
			FileInputStream is = new FileInputStream(filepath);
			byte buffer[] = new byte[is.available()];
			is.read(buffer);
			// log.error(new String(buffer));
			is.close();
			out.println((new String(buffer, "iso-8859-1"))
					.replaceAll("\"", " ").replaceAll("'", " "));// ����ļ�����
		}
	}

	public void init() throws ServletException {

	}

}
